<?php
// +----------------------------------------------------------------------
// | HkCms 自定义页面验证
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\template\validate;

use think\Validate;

class Customtemp extends Validate
{
    protected $rule = [
        'tempname' => 'require|checkPname',
        'temppath' => 'require|checkPath',
        'temptext' => 'require',
    ];

    protected $message = [
        'tempname.require'  =>  '自定义模板名称不能为空',
        'tempname.checkPname'  =>  '自定义页面文件名称有误',
        'temppath.require' =>  '路径不能为空',
        'temptext.require' =>  '页面内容不能为空',
        'temppath.checkPath' =>  '该路径已经存在自定义页面',
    ];

    // 检查相同路径的是否存在
    public function checkPath($value,$rule,$data)
    {
        $info = Db('customtemp')->where(['temppath'=>$data['temppath'],'tempname'=>$data['tempname']])->find();
        if ($info) {
            return false;
        } else {
            return true;
        }
    }

    // 自定义页面文件名称有误
    public function checkPname($value,$rule,$data)
    {
        $name = explode('.', $value);
        if (count($name) == 2) {
            return true;
        } else {
            return false;
        }
    }

    protected $scene = [
        'edit'  =>  ['tempname','temppath'=>'require','temptext'],
        'add'   =>  ['tempname','temppath','temptext']
    ];
}